package com.yswy.app.controller;

import com.yswy.app.dto.UploadImgVo;
import com.yswy.app.service.SysConfigService;
import com.yswy.common.model.ResultModel;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/config")
@Api(description = "常量")
public class ConfigController {
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private HuaWeiYunOBSUtil obsUtil;
    /**
     * 获取常量
     * @param key
     * @return
     */
    @RequestMapping(value = "getConFigByType", method = RequestMethod.GET)
    @ApiOperation(value = "获取常量")
    public ResultModel getConFigByType(@RequestParam(value = "key") String key){

        return ResultModel.ok(sysConfigService.selectConfigByKey(key));

    }

    /**
     * 获取常量
     * @param key
     * @return
     */
    @RequestMapping(value = "getConFigByTypeList", method = RequestMethod.GET)
    @ApiOperation(value = "获取常量List")
    public ResultModel getConFigByTypeList(@RequestParam(value = "key") String key){
        return ResultModel.ok(sysConfigService.selectConfigByKeyMap(key));
    }

    /** 获取图片上传链接 **/
    @PostMapping("/getUploadImgUrl")
    @ApiOperation(value = "获取图片上传链接")
    public ResultModel getUploadImgUrl(@RequestBody() UploadImgVo uploadImgVo) {
        List<String> list = new ArrayList<>();
        if (uploadImgVo.getNames().size() > 0) {
            for (String s:uploadImgVo.getNames()) {
                String suffix = s.substring(s.lastIndexOf(".") + 1);
                String fileNewName = UUID.randomUUID().toString();
                String str = obsUtil.getUploadSignUrl("goodsInfoFile/"+fileNewName+"."+suffix,null);
                list.add(str);
            }
        }
        return ResultModel.ok(list);
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "getSystemDate", method = RequestMethod.GET)
    @ApiOperation(value = "获取系统时间")
    public ResultModel getSystemDate(){
        return ResultModel.ok(new Date());
    }



}
